﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TechnicBlog.Models;

namespace TechnicBlog.Controllers.FireWorkManager
{
    public class NormController : Controller
    {
        //
        // GET: /Norm/

        public ActionResult Index(int id)
        {
            
            SetFireWorkAndBrand(id);
            return View();
        }

        private void SetFireWorkAndBrand(int fireWorkId)
        {
            DBContext db = new DBContext();
            FireWorkEntity fwModel = db.FireWorks.Find(fireWorkId);
            BrandEntity brandModel = db.Brands.Find(fwModel.BrandId);
            ViewBag.FireWork = fwModel;
            ViewBag.Brand = brandModel;
        }

        public string List(int id)
        {
            DBContext db = new DBContext();
            int pageindex = Convert.ToInt32(Request.Form["page"]);
            int pagesize = Convert.ToInt32(Request.Form["rows"]);
            IList<NormEntity> list = db.Norms.ToList();
            var result = from u in list
                         where u.FireWorkId.Equals(id)
                         orderby u.NormName ascending
                         select u;
            var l = (result).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList();
            int total = result.Count();
            Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            //这里使用自定义日期格式，如果不使用的话，默认是ISO8601格式           
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd";
            //jsonObject是准备转换的对象
            string s = "{\"total\":" + total + ",\"rows\":" + Newtonsoft.Json.JsonConvert.SerializeObject(l, Newtonsoft.Json.Formatting.Indented, timeConverter) + "}";
            db.Dispose();
            return s;
        }
        public ActionResult Create(int id)
        {
            SetFireWorkAndBrand(id);
            return View();
        }

        public ActionResult Add(NormEntity model)
        {
            try
            {
                DBContext db = new DBContext();
                db.Norms.Add(model);
                db.SaveChanges();
                db.Dispose();
                return JavaScript("$('#loading').window('close');$('#divNormEdit').window('close');$.messager.alert('提示','<br>恭喜您！新增成功！','info');$('#rptNormList').datagrid('reload');");
            }
            catch (Exception)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('错误','<br>新增失败！请检查输入是否有误！','error');");
            }
        }

        public ActionResult Edit(int id)
        {
            DBContext db = new DBContext();
            NormEntity model = db.Norms.Find(id);
            SetFireWorkAndBrand(model.FireWorkId);
            db.Dispose();
            if (model == null)
            {
                return JavaScript("$('#loading').window('close');$('#divNormEdit').window('close');$.messager.alert('提示','<br>该数据不存在或已被删除','info');$('#rptNormList').datagrid('reload');");
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(NormEntity model)
        {
            try
            {
                DBContext db = new DBContext();
                NormEntity brand = db.Norms.Find(model.Id);
                UpdateModel(brand);
                db.SaveChanges();
                db.Dispose();
                return JavaScript("$('#loading').window('close');$('#divNormEdit').window('close');$.messager.alert('提示','<br>恭喜您！编辑成功！','info');$('#rptNormList').datagrid('reload');");
            }
            catch (Exception)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('错误','<br>编辑失败！请检查输入是否有误！','error');");
            }
        }

        public ActionResult Delete(int id)
        {
            DBContext db = new DBContext();
            //int fwId = Convert.ToInt32(Request.QueryString["Id"].ToString());
            NormEntity model = db.Norms.Find(id);
            if (model == null)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>要删除的数据不存在，或已被删除！','info'); $('#rptNormList').datagrid('reload');");
            }
            db.Norms.Remove(model);
            db.SaveChanges();
            db.Dispose();
            return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>删除成功！','info');$('#rptNormList').datagrid('reload');");
        }
    }
}
